'''
    学生管理系统
    1。登陆银行系统
    2。查询余额
    3。取钱
    4。存钱
    5。退出银行系统
    6。界面和交互
'''

# 1。 银行信息源
person_list = [
    {'name':'user1', 'amount':100.0},
    {'name':'user2', 'amount':200.0},
    {'name':'user3', 'amount':300.0}
]

user_logged = -1

def show_person_info(user_id):
    '''

    :return:
    '''

    if int(user_id) > len(person_list):
        print("="*20,"没有用户信息", "="*20)
        return
    user = person_list[user_id]
    print('|{0:<10}|{1:<20}|'.format('用户','余额'))
    print("-"*45)
    print('|{0:<10}|{1:<20}|'.format(user["name"], user["amount"]))


def get_person_money(userid):
    user = person_list[userid]
    return user["amount"]


def add_pserson_money(userid, amount):
    updated = float(get_person_money(userid)) + amount
    person_list[userid]["amount"] = updated

def remove_pserson_money(userid, amount):
    leftAmount = float(get_person_money(userid))
    updatedMoney = 0
    if leftAmount > amount:
        updatedMoney = leftAmount - amount
        print("=" * 4, " 你一共取出了{} ".format(updatedMoney), "=" * 4)
    else:
        print("=" * 4, " 你一共取出了{} ".format(leftAmount), "=" * 4)

    person_list[userid]["amount"] = updatedMoney

def logout():
    global user_logged
    user_logged = -1
    print("="*4, " 再见 ", "="*4)


def login(userid):
    global user_logged
    try:
        userid = int(userid)
        if 0 <= userid - 1 < len(person_list):
            show_person_info(userid)
            user_logged = userid
            input("按回车继续：")
        else:
            print("=" * 4, " 不存在该用户 ", "=" * 4)
            input("按回车继续：")
    except ValueError:
        pass


while True:
    print("="*12, " 自动存款机管理系统 ", "="*12)
    if user_logged == -1:
        print('{0:<6}{1:<13}{2:15}'.format("", "1. 登陆", "2. 退出"))
        print("=" * 40)
        key = input("请输入对应的选择")
        if key == "1":
            userid = input("用户请输入卡号登陆(etc 1,2,3..)")
            login(userid)
        else:
            logout()
            break
    else:
        print('{0:<6}{1:<13}{2:15}'.format("", "1. 取钱", "2. 存钱"))
        print('{0:<6}{1:<13}{2:15}'.format("", "3. 显示余额", "4. 退出"))
        print("=" * 40)
        key = input("请输入对应的选择")
        if key == "1":
            amount = input("要取多少钱：")
            try:
                amount = float(amount)
                remove_pserson_money(user_logged, amount)
                show_person_info(user_logged)
                input("按回车继续：")
            except ValueError:
                print("="*6, " 输入错误 ", "="*6)
                input("按回车继续：")
        elif key == "2":
            amount = input("要存多少钱：")
            try:
                amount = float(amount)
                add_pserson_money(user_logged, amount)
                show_person_info(user_logged)
                input("按回车继续：")
            except ValueError:
                print("=" * 6, " 输入错误 ", "=" * 6)
                input("按回车继续：")
        elif key == "3":
            show_person_info(user_logged)
            input("按回车继续：")
        else:
            logout()
            break




